﻿using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ComLib.ConfigService;

namespace ProcessConfig.ConfigType
{
    public class PhotoDirectory : IniConfigBase
    {
        private string _localSavePath = @"D:\KPhoto";

        private System.IO.DirectoryInfo _currDirInfo = null;

        public string LocalSavePath
        {
            get { return _localSavePath; }
            set { _localSavePath = value; }
        }

        /// <summary>
        /// 本地共享目录（让其他电脑访问）
        /// </summary>
        private string _localSharedPath = @"D:\SharePhoto";
        public string LocalSharedPath
        {
            get { return _localSharedPath; }
            set { _localSharedPath = value; }
        }

        private void CreatRootDirectory()
        {
            if (!Directory.Exists(_localSavePath))
            {
                Directory.CreateDirectory(_localSavePath);
                Log.WriteAsync("创建新目录:" + _localSavePath);
            }
            _strRootSavePath = string.Format(@"{0}\{1}\{2}",
                   _localSavePath,
                   System.DateTime.Now.ToString("yyyyMM"),
                   System.DateTime.Now.ToString("dd"));
            _currDirInfo = new DirectoryInfo(_strRootSavePath);
            if (!_currDirInfo.Exists)
            {
                _currDirInfo.Create();
                Log.WriteAsync("创建新目录:" + _strRootSavePath);
            }
        }

        /// <summary>
        /// 检查并更新当日目录
        /// </summary>
        /// <returns></returns>
        private void CheckAndUpdateRootPath()
        {
            if (_currDirInfo == null)
            {
                CreatRootDirectory();
            }
            if (string.IsNullOrEmpty(_strRootSavePath))
            {
                CreatRootDirectory();
            }
            if (!Directory.Exists(_strRootSavePath))
            {
                CreatRootDirectory();
            }
            if (!_currDirInfo.CreationTime.Date.Equals(System.DateTime.Now.Date))
            {
                CreatRootDirectory();
            }
        }

        /// <summary>
        /// 当日照片保存的根目录
        /// </summary>
        private string _strRootSavePath = "";
        public string RootSavePath
        {
            get
            {
                CheckAndUpdateRootPath();
                return _strRootSavePath;
            }
        }

        /// <summary>
        /// 实时预览图像保存路径
        /// </summary>
        public string Sync
        {
            get
            {
                return CheckAndCreatPath("SYNC");
            }
        }

        /// <summary>
        /// 临时照片(调整后，质检前)文件夹名称
        /// </summary>     
        public string Tmp
        {
            get
            {
                return CheckAndCreatPath("TMP");
            }
        }

        /// <summary>
        /// 质检成功照片保存文件夹名称
        /// </summary>
        public string QCPass
        {
            get
            {
                return CheckAndCreatPath("PASS");
            }
        }

        /// <summary>
        /// 质检失败照片保存文件夹名称
        /// </summary>
        public string QCNoPass
        {
            get
            {
                return CheckAndCreatPath("NOTPASS");
            }
        }
        /// <summary>
        /// 输出成果照片保存文件夹名称
        /// </summary>
        public string Outcome
        {
            get
            {
                return CheckAndCreatPath("OUTCOME");
            }
        }

        /// <summary>
        /// 照片光滑处理后保存文件夹名称
        /// </summary>
        public string Smooth
        {
            get
            {
                return CheckAndCreatPath("SMOOTH");
            }
        }

        /// <summary>
        /// 原照片保存文件夹名称
        /// </summary>
        public string Orig
        {
            get
            {
                return CheckAndCreatPath("ORG");
            }
        }

        /// <summary>
        /// 裁切后照片保存文件夹名称
        /// </summary>
        public string Crop
        {
            get
            {
                return CheckAndCreatPath("CROP");
            }
        }

        /// <summary>
        /// 余宏春处理后照片名称
        /// </summary>
        public string CBK
        {
            get
            {
                return CheckAndCreatPath("CBK");
            }
        }

        /// <summary>
        /// 打印照片保存文件夹名称
        /// </summary>
        public string Print
        {
            get
            {
                return CheckAndCreatPath("PRINT");
            }
        }

        /// <summary>
        /// 错误照片保存文件夹名称
        /// </summary>
        public string ErrImg
        {
            get
            {
                return CheckAndCreatPath("ERROR");
            }
        }

        /// <summary>
        /// 照片(添加了备注信息的)保存文件夹名称
        /// </summary>
        public string Remarks
        {
            get
            {
                return CheckAndCreatPath("REMARKS");
            }
        }

        private string CheckAndCreatPath(string name)
        {
            string path = Path.Combine(RootSavePath, name);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                Log.WriteAsync("创建新目录:" + path);
            }
            return path;
        }

        public override bool Save()
        {
            base.Save();
            SaveField("LocalSavePath", _localSavePath);
            SaveField("LocalSharedPath", _localSharedPath);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _localSavePath = LoadStringValue("LocalSavePath");
                _localSharedPath = LoadStringValue("LocalSharedPath");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("照片工作目录选项::");
            builder.Append("[本地工作目录]: " + _localSavePath);
            builder.Append("[本地共享目录]: " + _localSharedPath);
            return builder.ToString();
        }
    }
}
